//
// File:	RenderCanvas.cpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Create:	29/01/2010 at 02:20:44
// Purpose:	
//
#include <vector>
#include <SFML/Graphics.hpp>
#include "Base.hpp"
#include "RenderCanvas.hpp"
#include "IEntityManager.hpp"
//---TEMP DELETE ME---
#include "ResourceManager.hpp"
#include "ResourceManagerTypes.hpp"
//---
#include "Trace.hpp"

//------------------------------------------------------------------
// Constructor / Destructor
//------------------------------------------------------------------
RenderCanvas::RenderCanvas(QWidget *pParent /* = NULL */, QSize canvasSize /* = QSize() */, unsigned int iFrameTime /* = 0 */)
	:	AbstractRenderCanvas(pParent, iFrameTime),
		m_originalSize(canvasSize)
{
	resize(canvasSize);
}

RenderCanvas::~RenderCanvas()
{
}

//------------------------------------------------------------------
// Init
//------------------------------------------------------------------
void RenderCanvas::Init()
{
	AbstractRenderCanvas::Init();
}

//------------------------------------------------------------------
// Update
//------------------------------------------------------------------
void RenderCanvas::Update()
{
	//Clear Screen
	Clear();

	//Get the view size ratio related to the window real size / window reference size
	int iWinWidth = GlobalVariables::DisplayManager::Get().GetWindow()->GetWidth();
	int iWinHeight = GlobalVariables::DisplayManager::Get().GetWindow()->GetHeight();
	int iWinRefWidth = GlobalVariables::DisplayManager::Get().GetWindow()->GetReferenceWidth();
	int iWinRefHeight = GlobalVariables::DisplayManager::Get().GetWindow()->GetReferenceHeight();
	float flRefScaleX = static_cast<float>(iWinWidth) / static_cast<float>(iWinRefWidth);
	float flRefScaleY = static_cast<float>(iWinHeight) / static_cast<float>(iWinRefHeight);

	//Setup View
	sf::Vector2f viewCenter, viewHalfRect;
	viewCenter.x = GlobalVariables::DisplayManager::Get().GetView()->GetViewCenterX() * flRefScaleX;
	viewCenter.y = GlobalVariables::DisplayManager::Get().GetView()->GetViewCenterY() * flRefScaleY;
	viewHalfRect.x = GlobalVariables::DisplayManager::Get().GetView()->GetViewWidth() * flRefScaleX * 0.5f;
	viewHalfRect.y = GlobalVariables::DisplayManager::Get().GetView()->GetViewHeight() * flRefScaleY * 0.5f;
	SetView(sf::View(viewCenter, viewHalfRect));

	//Render Entities
	GlobalVariables::EntityManager::Get().RenderEntities();

	//---TEST HUD---
	class IHudElement : public IRenderable
	{
	public:
		virtual				~IHudElement() {}

	public:
		//IRenderable > virtual void		Update() = 0;
		//IRenderable > virtual void		Render(float flX, float flY) = 0;
		//IRenderable > virtual void		SetRenderColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255) = 0;
		
		virtual void		SetX(float) = 0;
		virtual void		SetY(float) = 0;
		virtual void		SetWidth(float) = 0;
		virtual void		SetHeight(float) = 0;
		//virtual void		SetColor

	public:
		virtual void		GetX() const = 0;
		virtual void		GetY() const = 0;
		virtual void		GetWidht() const = 0;
		virtual void		GetHeight() const = 0;
	};
	sf::Vector2f vfProgressPosition(100, 100);
	sf::Vector2f vfProgressSize(200, 16);
	sf::Vector2f vfProgressP1(vfProgressPosition);
	sf::Vector2f vfProgressP2(vfProgressPosition + vfProgressSize);
	float flProgressOutlineSize = 1.0f;
	sf::Color progressOutlineColor(128, 128, 128, 128);
	sf::Color progressBackgroundColor(0, 0, 0, 0);
	sf::Shape progressBarContainer = sf::Shape::Rectangle(vfProgressP1, vfProgressP2, progressBackgroundColor, flProgressOutlineSize, progressOutlineColor);
	progressBarContainer.Move(viewCenter - viewHalfRect);
	Draw(progressBarContainer);

	sf::String text("Dolphin's Eye", sf::Font::GetDefaultFont(), 18.0f);
	text.Move(viewCenter - viewHalfRect);
	Draw(text);
	//---
}

//------------------------------------------------------------------
// Draw
//------------------------------------------------------------------
void RenderCanvas::Draw(const sf::Drawable &drawable)
{
	//Fix the aspact ratio
	float flOriginalWidth = m_originalSize.height();
	float flOriginalHeight= m_originalSize.width();
	sf::Drawable &nonConst = const_cast<sf::Drawable &>(drawable);
	nonConst.Scale(flOriginalHeight / flOriginalWidth, flOriginalWidth / flOriginalHeight);
	AbstractRenderCanvas::Draw(nonConst);
}
